package com.guigu.zj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.MealgroupMapper;
import com.guigu.mapper.SerialcodingMapper;
import com.guigu.mapper.SetmealMapper;
import com.guigu.pojo.Mealgroup;
import com.guigu.pojo.Serialcoding;
import com.guigu.pojo.Setmeal;
import com.guigu.zj.pojo.PageVo;
import com.guigu.zj.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MealServiceImpl implements MealService {
    @Autowired
    SetmealMapper meal;
    @Autowired
    MealgroupMapper mealgroupMapper;
    @Autowired
    SerialcodingMapper serialcodingMapper;
    public int insertSelective(Setmeal record) {
        Serialcoding ser = serialcodingMapper.find();
        record.setBm("tc00"+(ser.getMealid()+1));
        Serialcoding s = new Serialcoding();
        s.setMealid(ser.getMealid()+1);
        serialcodingMapper.update(s);
        return meal.insertSelective(record);
    }


    public int insertSelective(Mealgroup record) {
        return mealgroupMapper.insertSelective(record);
    }

    public PageVo<Setmeal> queryAll(PageVo<Setmeal> pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getNum());
        List<Setmeal> setmeals = meal.queryAll(pageVo);
        PageInfo pageInfo = new PageInfo(setmeals);
        long total = pageInfo.getTotal();
        PageVo<Setmeal> vo = new PageVo<Setmeal>();
        vo.setTotal(total);
        vo.setRows(setmeals);
        return vo;
    }

    public Setmeal selectByPrimaryKey(Integer id) {
        return meal.selectByPrimaryKey(id);
    }

    public List<Mealgroup> selectBy(Integer id) {
        return mealgroupMapper.selectBy(id);
    }

    public int updateByPrimaryKeySelective(Setmeal record) {
        return meal.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKeySelective(Mealgroup record) {
        return mealgroupMapper.updateByPrimaryKeySelective(record);
    }

    public int deleteByPrimaryKey(Integer id) {
        int i = meal.deleteByPrimaryKey(id);
        if(i>0){
            int del = mealgroupMapper.del(id);
        }
        return i;
    }

}
